package com.vencent.spring;

import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * config DispatcherServlet
 * @author wbw
 *
 */
public class WebApplication extends AbstractAnnotationConfigDispatcherServletInitializer{

	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class<?>[]{RootConfig.class};
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class<?>[]{WebConfig.class};
	}

	@Override
	protected String[] getServletMappings() {
		return new String[]{"/"};
	}
	
	/**
	 * MultipartConfigElement is better than CommonsMultipartResolver, it only relys on servlet.
	 * normal post request's result is simply likely : firstName = 1&secName =2&lastName=3
	 * for Binary data, need multipart's request, it split form to mult part.
	 */
	@Override
	protected void customizeRegistration(Dynamic registration) {
		registration.setLoadOnStartup(1);
		/** the first param is tempPath, second param is fileSize, 
		 *  the third param is maxRequestSize, last param is memory's size*/
		registration.setMultipartConfig(new MultipartConfigElement("/temp/uploads", 2*1024*1024, 4*1024*1024, 0));
	}
	
	@Override
	protected Filter[] getServletFilters() {
		return new Filter[]{new MyFilter()};
	}
}
